//
//  LDBossInfoNet.swift
//  DingDongZP
//
//  Created by 三秒文化 on 16/6/2.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//

import UIKit

class LDBossInfoNet: NSObject {
    
    
    //入口
    class var sharedInstance:LDBossInfoNet {
        struct Staics {
            static let instance = LDBossInfoNet()
        }
        return Staics.instance
    }
    
    //查询融云token
    func queryToken(success:((data:String)->())?,failure:((data:String)->())?){
        
        LDNetWorkManager.sharedInstance.LoginPOST(LDEmploryAPI.query.rongyunToken, parameters: nil, success: { (value) in
            
            let token = value["token"] as? String ?? LDNull
            
            // 获取token的接口缓存用户姓名
            DDLoginUserManager.setAccountNamefirst(value["name"] as? String ?? "")
            // 获取token的接口缓存用户头像
            DDLoginUserManager.setDdportrait(value["logo"] as? String ?? "")
            if success != nil {
               success!(data: token)
            }
            
            
        }) { (data, error) in
            
            if failure != nil {
                
                failure!(data: data as? String ?? "")
            }
            
        }
        
    }
    
    //搜索 筛选 ＋ 文本
    /** filterid,filtertype 暂时穿空 */
    func searchJobFinders(page page:Int,filterid:Int?,filtertype:String?,text:String,
                               success:((hasNextPage:Int,jobsArr:[NSDictionary])->Void),failure:((data:AnyObject)->())?) {
        
        var params = NSDictionary()
        
        if filtertype != nil {
            
            params = ["filterid":"\(filterid ?? 0)","filtertype":filtertype!,"page":"\(page)","search":text]
        } else {
            
            params = ["page":"\(page)","search":text]
        }
        
        
        LDNetWorkManager.sharedInstance.LoginPOST(LDEmploryAPI.discover.jobfinders_search, parameters: params as? Dictionary<String, AnyObject>, success: { (value) in
            
            let data = value as? NSDictionary
            
            let hasNextPage = data!["hasNextPage"] as? Int ?? 0
            let jobsArr = data!["jobs"] as? [NSDictionary] ?? [NSDictionary]()
            
            success(hasNextPage: hasNextPage,jobsArr: jobsArr)
            
            
        }) { (data, error) in
            
            if failure != nil {
                
                failure!(data: data)
            }
        }
        
    }
    
    //查询某个牛人微简历
    
    func queryJobFinderMyResume(userid:Int, success:(dict:Dictionary<String, AnyObject>)->Void,failure:((error:
        AnyObject)->Void)?) {
        
        
        let dict = ["jobfinder_id":userid]
        LDNetWorkManager.sharedInstance.LoginPOST(LDEmploryAPI.query.jobfinders_resume, parameters: dict, success: {
            (value) in
            
            let myRemuseInfoDict = value as! [String : AnyObject]
            success(dict: (myRemuseInfoDict))
            
        }) { (data, error) in
            
            if failure != nil {
                failure!(error: data)
            }
            LDShowHUB.shareInstance.showBarStatus(data as! String)
            
        }
    }
    
    //查询发布的所有职位
    
    func viewMyselfAllJobsKinds(success:(dictArr:[NSDictionary])->Void,failure:((error:
        AnyObject)->Void)?) {
        
        let api = LDEmploryAPI.view.allPublishJobs
        
        //        let dictArr = LDCacheManager.shareInstance.userCacheManger?.objectForKey("employallpublishedjobs")
        
        //        if dictArr != nil {
        //            success(dictArr: dictArr as! [NSDictionary])
        //        }
        
        
        LDNetWorkManager.sharedInstance.LoginPOST(api, parameters: nil, success: { (value) in
            
            
            let dictArr = value as? [NSDictionary] ?? [NSDictionary]()
            
            LDCacheManager.shareInstance.saveToCache("employallpublishedjobs", value: dictArr, success: nil)
            success(dictArr: dictArr)
            
            
        }) { (data, error) in
            
            LDCacheManager.shareInstance.readFromCache("employallpublishedjobs", colsure: { (key, value) in
                
                if value != nil {
                    
                    let dictArr = value as? [NSDictionary] ?? [NSDictionary]()
                    success(dictArr: dictArr)
                    
                } else {
                    
                    failure!(error: data)
                }
                
            })
        }
    }
    
    //查询轮播图
    func queryEmployerAds(success:(dictArr:[String])->Void,failure:((error: AnyObject)->Void)?) {
        
        LDNetWorkManager.sharedInstance.LoginPOST(LDEmploryAPI.query.query_ads, parameters: nil, success: { (value) in
            
            let data = value as? [String] ?? [String]()
            
            let dataUrl = data.map{LDCommentAPI.getPicApi($0)}
            
            success(dictArr: dataUrl)
            
            LDCacheManager.shareInstance.saveToCache("employadsimg", value: data, success: nil)
            
        }) { (data, error) in
            
            LDCacheManager.shareInstance.readFromCache("employadsimg", colsure: { (key, value) in
                
                if value != nil {
                    
                    success(dictArr: value! as! [String])
                } else {
                    
                    if failure != nil {
                        
                        failure!(error: data)
                    }
                }
            })
        }
    }
    //查询个人信息
    
    func queryProfileInfo(success:((dict:NSDictionary)->Void),failure:((error:AnyObject)->Void)?) {
        
        LDNetWorkManager.sharedInstance.LoginPOST(LDEmploryAPI.query.query_profile, parameters: nil, success: {
            (value) in
            
            let dict = value as? Dictionary<String,AnyObject> ?? [String:AnyObject]()
            
            LDCacheManager.shareInstance.saveToCache("employerProfileInfo", value: dict, success: nil)
            success(dict: dict)
            
        }) { (data, error) in
            
            LDCacheManager.shareInstance.readFromCache("employerProfileInfo", colsure: { (key, value) in
                
                if value != nil {
                    
                    let dict = value as? NSDictionary ?? NSDictionary()
                    success(dict: dict)
                } else {
                    
                    if failure != nil {
                        
                        failure!(error:data)
                    }
                }
            })
            
//            LDShowHUB.shareInstance.showBarStatus("请求失败...")
            
        }
    }
    
    
    //查询个推设置
    func queryGeTuiPushSetting(success:((dict:NSDictionary)->Void),failure:((error:AnyObject)->Void)?) {
        
        let api = LDEmploryAPI.query.getuiPush
        
        LDNetWorkManager.sharedInstance.LoginPOST(api, parameters: nil, success: { (value) in
            
            success(dict: value as! NSDictionary)
            
        }) { (data, error) in
            
            if failure != nil {
                
                failure!(error: data)
            }
            
        }
    }
    //设置个推设置
    func setGetuiPushSetting(params:NSDictionary,success:(()->Void),failure:((error:AnyObject)->Void)?) {
        
        let api = LDEmploryAPI.set.getuiPush
        
        LDNetWorkManager.sharedInstance.LoginPOST(api, parameters: params as? Dictionary<String, AnyObject>, success: { (value) in
            
            success()
            
        }) { (data, error) in
            
            if failure != nil {
                
                failure!(error: data)
            }
        }
    }
    //Boss 端查询公司主页信息
    func queryCompanyInfo(success:((dict:NSDictionary)->Void),failure:((error:AnyObject)->Void)?) {
        
        let api = LDEmploryAPI.query.companyInfo
        
        LDNetWorkManager.sharedInstance.LoginPOST(api, parameters: nil, success: { (value) in
            
            LDCacheManager.shareInstance.saveToCache("querycompanyinfo", value: value as! NSDictionary, success: nil)
            success(dict: value as! NSDictionary)
            
            
        }) { (data, error) in
            
            
            LDCacheManager.shareInstance.readFromCache("querycompanyinfo", colsure: { (key, value) in
                
                if value != nil {
                    
                    success(dict: value as? NSDictionary ?? NSDictionary())
                } else {
                    
                    if failure != nil {
                        failure!(error: data)
                    }
                }
                
            })
        }
    }
    //Boss 端查看自己发布的所有职位
    
    func queryAllPublishJob(success:((dictArr:[NSDictionary])->Void),failure:((error:AnyObject)->Void)?) {
        
        let api = LDEmploryAPI.query.alljobs
        
        LDNetWorkManager.sharedInstance.LoginPOST(api, parameters: nil, success: { (value) in
            
            LDCacheManager.shareInstance.saveToCache("queryallpublishcompanyinfo", value: value as! [NSDictionary], success: nil)
            success(dictArr: value as! [NSDictionary])
            
            
        }) { (data, error) in
            
            
            LDCacheManager.shareInstance.readFromCache("queryallpublishcompanyinfo", colsure: { (key, value) in
                
                if value != nil {
                    
                    success(dictArr: value as? [NSDictionary] ?? [NSDictionary]())
                } else {
                    
                    if failure != nil {
                        failure!(error: data)
                    }
                }
                
            })
        }
    }
    //122. Boss 端举报牛人
    func reportJobfinder(params:[String:String],success:(()->Void),failure:((error:AnyObject)->Void)?) {
        
        let api = LDEmploryAPI.set.reportJobfinder
        
        LDNetWorkManager.sharedInstance.LoginPOST(api, parameters: params, success: { (value) in
            
            success()
            
        }) { (data, error) in
            
            if failure != nil {
                
                failure!(error: data)
            }
        }
    }
    //
    private override init() {
        super.init()
    }
}
